﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WISD.Common;
using WISD.CarFriend.Model;
using WISD.CarFriend.BLL.OA;

namespace WISD.CarFriend.Web.OA
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            this.LoginAdmin();
        }

        private void LoginAdmin()
        {
            try
            {
                string userName = this.txtName.Text.Trim();
                string userPassword = this.txtPassword.Text.Trim();
                string memberId = string.Empty;
                if (userName.Equals("") || userPassword.Equals(""))
                {
                    this.lblMessage.Text = "请输入登录用户名或密码";
                }
                else
                {
                    if (Session["CountOfLogin"] == null)
                    {
                        Session["CountOfLogin"] = 1;
                    }
                    else
                    {
                        Session["CountOfLogin"] = Convert.ToInt32(Session["CountOfLogin"]) + 1;
                    }
                } //判断登录
                if (Session["CountOfLogin"] != null && Convert.ToInt32(Session["CountOfLogin"]) > 3)
                {
                    this.lblMessage.Text = "登录错误超过3次，请关闭浏览器重新登录。";
                }
                else if (WebModule.ValidLogin(userName, userPassword, out memberId))
                {
                    Session["OA_MemberID"] = memberId;
                    Session["OA_WebUserName"] = userName;
                    Session.Timeout = 45;    //设置超时时间
                    Session["CountOfLogin"] = null;
                    MessageBox.AlertAndGo(this.Page, userName + "用户登陆成功！", "Index.aspx");
                }
                else
                {
                    this.lblMessage.Text = "您输入的用户名或密码不正确";
                }
            }
            catch (Exception ex)
            {
                this.lblMessage.Text = "登录出错，请联系管理员。";
            }
        }
    }
}